package bandwidthcontroller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ppdbuilder
 */
public class TCOperations {
    
    
    String iFace ;
    CommandGenerator generator ;
    Runtime runtime;
    Process process;
    
    public TCOperations(String tool){
        
        if(tool.contains("DummyNet"))
            this.generator = new DummyNetCommandGenerator();
        else
            this.generator = new TCCommandGenerator();
        runtime = Runtime.getRuntime();
    }
    
    public void setBandWidthAllIP(BandwidthSettings bw){
        try {
            System.out.println("SET ALL BANDWIDHT COMMANDS");
            if(this.isIngressDefined())
                this.clearAllBandwidth(bw);
            String[] cmdArray = generator.getSetAllBandwidthCommand(bw);
            for(String temp: cmdArray)
                System.out.println(temp);
            process = runtime.exec("sudo ifconfig -a");
            System.out.println("ifconfig:" + process.waitFor());
            
            executeTCCommand(cmdArray);
        } catch (InterruptedException ex) {
            Logger.getLogger(TCOperations.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(TCOperations.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
    }
    
    public void clearAllBandwidth(BandwidthSettings bw){
        
        System.out.println("CLEAR ALL BANDWIDTH");
        String[] cmdArray = generator.getClearAllBandwidthCommand(bw);
        for(String temp: cmdArray)
            System.out.println(temp);
        executeTCCommand(cmdArray);
       
    }
    
    
    public void executeTCCommand(String[] cmdArray){
        int exitVal = -1;
        
        try {
            for(String cmd: cmdArray){
                process = runtime.exec(cmd);
                exitVal = process.waitFor();
                Logger.getLogger(this.getClass().getName()).log(Level.INFO, "COMMAND:{0} exitvalue:{1}", new Object[]{cmd, exitVal});
                BufferedReader lines = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String line = lines.readLine();
                while(line!=null){
                    System.out.println(line);
                    line = lines.readLine();
               }
            }
            if(exitVal == 0)
            {
                System.out.println("PROCESS TERMINATED WITH VAL:"+exitVal);
                Logger.getLogger(this.getClass().getName()).log(Level.FINE, "TC Processes exited Normally");
            }
        } catch (InterruptedException ex) {
            Logger.getLogger(TCOperations.class.getName()).log(Level.SEVERE, "TC Process did not finish properly cannot set bandwidth", ex);
        } catch (IOException ex) {
            Logger.getLogger(TCOperations.class.getName()).log(Level.SEVERE, "Could not execute the TC Commands on the shell", ex);
        }
        
    }
    
    boolean isIngressDefined(){
        try {
            String command = "sudo tc qdisc show dev " + this.iFace ;
            boolean isIngress = false;
            process = runtime.exec(command);
            process.waitFor();
            
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = reader.readLine();
            String pattern = "ingress";
            
            while(line != null){
                System.out.print(line);
                isIngress = line.contains(pattern);
                if(isIngress)
                    break;
                line = reader.readLine();
            }
            return isIngress;
        } catch (InterruptedException ex) {
            Logger.getLogger(TCOperations.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        } catch (IOException ex) {
            Logger.getLogger(TCOperations.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }
    
}